﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once

#include "GcmpDevService.h"
#include <string>
#include <vector>

namespace gcmp
{
    class DirectoryEntry;
    class GCMP_DEV_SERVICE_EXPORT FileUtility
    {
    private:
        FileUtility(void);

    public:
        static bool CopyFileW(const std::wstring& srcFile,const std::wstring& destFile, bool bFailIfExists = false);

        static std::wstring GetAvailableFilePath(const std::wstring& fileFolder, const std::wstring& prefix, const std::wstring& ext);

        static std::vector<char> Load(const std::wstring &filePath);

        static std::vector<char> LoadInBinaryMode(const std::wstring &filePath);

        static std::string LoadINIFile(const std::wstring &filePath);

        static bool Save(const std::wstring &filePath, const char *data, int dataSize);

        static bool SaveInBinaryMode(const std::wstring &filePath, const char *data, int dataSize,bool isAppend = false);

        // 创建目录，可以一次递归创建多级。
        // 存在或者创建成功，返回true；否则返回false
        // param showDebugWarning，出错时是否弹出调试警告，默认弹出；调用者可自行控制
        static bool CreateDirectories(const std::wstring &path, bool showDebugWarning = true);
        // 删除文件或者目录，如果是目录则递归删除，返回删除文件和目录的数量
        static int RemoveAll(const std::wstring &path);
        static int RemoveAll(const std::wstring &path, const std::function<bool(const DirectoryEntry &entry)> &filter);
        // 复制文件或者目录
        static void Copy(const std::wstring& srcPath,const std::wstring& destPath);
        static void Copy(const std::wstring& srcPath,const std::wstring& destPath, const std::function<bool(const DirectoryEntry &entry)> &filter);
        //获取文件夹下所有指定类型的的文件(非递归)
        static std::vector<std::wstring> GetFiles(const std::wstring &path,const std::wstring &ext);
        //按过滤器获取文件夹下的的文件全路径(默认非递归)
        static std::vector<std::wstring> GetFilePathsByFilter(const std::wstring &path, std::function<bool(const DirectoryEntry &entry)> filter, bool recursively = false);

        static bool AppendHideAttribute(const std::wstring& filePath);

        static bool RemoveHideAttribute(const std::wstring& filePath);
        static bool GetDiskFreeSpaceValue(const wchar_t* path, unsigned long long* freeSpace);
    private:
        // 路径存在或者创建成功，返回true；否则返回false
        static bool CreateDirectoriesImpl(const std::wstring &path, bool showDebugWarning);
        static int RemoveAllImpl(const std::wstring &path, const std::function<bool(const DirectoryEntry &entry)> &filter);
        static void CopyImpl(const std::wstring& srcPath,const std::wstring& destPath, const std::function<bool(const DirectoryEntry &entry)> &filter);
        
    };
}

